package com.dm.hdfxxt.loginmanage.controller;

import com.dm.hdfxxt.loginmanage.service.LoginService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = "goLogin")
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;


    /*####################################################################################*/
    /*##############################   登陆管理    #######################################*/
    /*####################################################################################*/
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String, Object> goLogin(@RequestBody Map<String, Object> param, HttpServletRequest request) {
        Map<String, Object> ResultMap = new HashMap<>();
        Map<String, Object> UserMap = loginService.queryUserByUserNo(param);
        HttpSession session = request.getSession();
        if (UserMap == null) {
            ResultMap.put("Status", -1); //用户名不存在
            return ResultMap;
        } else {
            String InputPwd = param.get("Password").toString();
            String QueryPwd = UserMap.get("Password").toString();
            if (InputPwd.equals(QueryPwd)) {
                UserMap.put("Password", "");
                UserMap.put("Status", 1); //登陆成功
                //存入用户session
                String QueryUserNo = UserMap.get("UserNo").toString();
                String QueryUserName = UserMap.get("UserName").toString();
                String QueryStore = UserMap.get("Store").toString();
                String QueryBm = UserMap.get("Bm").toString();
                String QueryLim = UserMap.get("Lim").toString();
                session.setAttribute("UserNo", QueryUserNo);
                session.setAttribute("UserName", QueryUserName);
                session.setAttribute("Store", QueryStore);
                session.setAttribute("Bm", QueryBm);
                session.setAttribute("Lim", QueryLim);
                session.setMaxInactiveInterval(60 * 60 * 4);
                return UserMap;
            } else {
                ResultMap.put("Status", -2); //密码输入错误
                return ResultMap;
            }
        }

    }

    /**
     * @Description 初始化index界面 查看是否存有session
     * @Author zhangxue
     * @Date 16:29 2018/7/11
     * @Param
     * @retrun
     **/
    @RequestMapping(value = "/initIndexPage", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String initPage(HttpSession session) {
        String UserNo = session.getAttribute("UserNo") != null ? session.getAttribute("UserNo").toString() : null;
        String UserName = session.getAttribute("UserName") != null ? session.getAttribute("UserName").toString() : null;
        String Store = session.getAttribute("Store") != null ? session.getAttribute("Store").toString() : null;
        String Bm = session.getAttribute("Bm") != null ? session.getAttribute("Bm").toString() : null;
        String Lim = session.getAttribute("Lim") != null ? session.getAttribute("Lim").toString() : null;
        Map<String, Object> tMap = new HashMap<>();
        tMap.put("UserNo", UserNo);
        tMap.put("UserName", UserName);
        tMap.put("Store", Store);
        tMap.put("Bm", Bm);
        tMap.put("Lim", Lim);
        JSONObject js = new JSONObject();
        if (UserNo == null || UserName == null || Store == null || Bm == null || Lim == null) {
            tMap.put("Status", "0");
            js = JSONObject.fromObject(tMap);
            return js.toString();
        } else {
            tMap.put("Status", "1");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String NowStr = sdf.format(new Date());
            tMap.put("now", NowStr);
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            String NowStr1 = sdf1.format(new Date());
            tMap.put("nowdate", NowStr1);
            js = JSONObject.fromObject(tMap);
            return js.toString();
        }
    }
}
